/**
 * @Title: PreviewPagerAdapter.java
 * @Package: com.jason.bluetooth.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月22日 下午9:39:58
 * @Version: V1.0
 */
package net.hzlj.platform.enterprise.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import net.hzlj.rl.zhangjiajie.R;
import net.hzlj.platform.enterprise.widght.RecyclingImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @ClassName: PreviewPagerAdapter
 * @Description: 图片预览器适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2016-01-18 11:31:35
 */
public class PreviewPagerAdapter extends PagerAdapter {


	private static final String TAG = PreviewPagerAdapter.class.getSimpleName();
	
	private DisplayImageOptions mDisplayImageOptions;
	private LayoutInflater mLayoutInflater;
	private Context mContext;
	private String[] mImages;

	public PreviewPagerAdapter(Context context, String[] image, DisplayImageOptions displayImageOptions){
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		this.mImages = image;
		this.mDisplayImageOptions = displayImageOptions;
	}

	public PreviewPagerAdapter(Context context, String[] image){
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		this.mImages = image;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.view.PagerAdapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mImages.length;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View, java.lang.Object)
	 */
	
//	private View imageLayout = null;
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	public View instantiateItem(ViewGroup container, int position) {

		View imageLayout = mLayoutInflater.inflate(R.layout.adapter_pager_image_item, container, false);
		RecyclingImageView imageView =  (RecyclingImageView) imageLayout.findViewById(R.id.image);
		ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
		String imagePath = mImages[position];
		Log.i(TAG, "imagePath:" + imagePath);
//		final ProgressBar spinners = spinner;
		spinner.setVisibility(View.GONE);
		Glide.with(mContext).load(imagePath)
				.centerCrop()
				.diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView);
//		if (imagePath.contains("http://") || imagePath.contains("https://")){
//			ImageLoader.getInstance().displayImage(imagePath,
//					imageView, mDisplayImageOptions);
//		}else{
//			ImageLoader.getInstance().displayImage("file://" + imagePath,
//					imageView, mDisplayImageOptions);
//		}
		try {
			((ViewPager) container).addView(imageLayout);
		} catch (Exception e) {
			// TODO: handle exception
		}
//		container.addView(imageLayout, 0);
		return imageLayout;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}
	
	
	class ViewHolder{
		RecyclingImageView imageView;
		ProgressBar spinner;
	}

}
